package com.example.opengldemo.simpleGame.catcherFun;


import android.content.Context;
import android.opengl.EGLConfig;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;


import com.example.opengldemo.R;
import com.example.opengldemo.simpleGame.MatrixState.MatrixState2D;
import com.example.opengldemo.simpleGame.MatrixState.MatrixState3D;
import com.example.opengldemo.simpleGame.util.manager.ShaderManager;
import com.example.opengldemo.simpleGame.view.BNAbstractView;
import com.example.opengldemo.simpleGame.view.GameView;
import com.example.opengldemo.simpleGame.view.LoadView;
import com.example.opengldemo.simpleGame.view.MainView;
import com.example.opengldemo.simpleGame.view.MenuView;

import javax.microedition.khronos.opengles.GL10;

import static com.example.opengldemo.simpleGame.constant.SourceConstant.*;

public class MySurfaceView extends GLSurfaceView {


    public MainActivity activity;
    private SceneRenderer mRenderer;

    public BNAbstractView currView; //当前功能
    public BNAbstractView collectionview; //奖品收藏
    public BNAbstractView YXJXView; //
    public BNAbstractView GameAboutView; //关于游戏
    public BNAbstractView ScoreView; //成绩总览

    public GameView gameView; //开始游戏
    public MainView mainView;   // 主页面
    public MenuView menuview;   // 菜单

    private static boolean isExit = false;
    public boolean isInitOver = false;

    public MySurfaceView(Context context) {
        super(context);
        activity = (MainActivity) context;
        this.setEGLContextClientVersion(3);

        mRenderer = new SceneRenderer();
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);

    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        if (currView == null) {
            return false;
        }
        return currView.onTouchEvent(e);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("MySurfaceView" , keyCode +">>"+event.getAction()) ;
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (currView == gameView) {
                if (gameView.isMenu) {
                    currView = gameView;
                    gameView.isMenu = false;
                } else {
                    currView = mainView;
                }

            } else if (currView == ScoreView) {
                currView = mainView;
            } else if (currView == YXJXView) {
                currView = mainView;
            } else if (currView == GameAboutView) {
                currView = mainView;
            } else if (currView == collectionview) {
                if (isCollection) {
                    currView = gameView;
                    isCollection = false;
                    gameView.isMenu = false;
                    gameView.reData();
                } else {
                    currView = mainView;
                }
                if (isSet) {
                    isSet = false;
                    currView = mainView;
                }

            } else if (currView == mainView) {
                if (isSet) {
                    isSet = false;
                    currView = mainView;
                } else {
                    exit();
                }

            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (isExit == false) {
            isExit = true;
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    isExit = false;
                    isBGMusic = true;
                    effictOff = true;
                }
            }, 2500);
        } else {
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }

    private class SceneRenderer implements Renderer {
        public void onDrawFrame(GL10 gl) {
            GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
            if (currView != null) {
                currView.drawView(gl);
            }

        }


        @Override
        public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) {

            GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            GLES30.glEnable(GL10.GL_CULL_FACE);
            ShaderManager.loadCodeFromFile(activity.getResources());
            ShaderManager.compileShader();
        }

        public void onSurfaceChanged(GL10 gl, int width, int height) {
            GLES30.glViewport(0, 0, width, height);

            float ratio = (float) width / height;
            screenWidth = width;
            screenHeight = height;
            MatrixState3D.setInitStack();
            MatrixState3D.setProjectFrustum(-ratio, ratio, -1, 1, 1.5f, 100);
            MatrixState3D.setCamera(
                    EYE_X, EYE_Y, EYE_Z,
                    TARGET_X, TARGET_Y, TARGET_Z,
                    0, 1, 0);

            MatrixState2D.setInitStack();
            MatrixState2D.setCamera(0, 0, 5, 0f, 0f, 0f, 0f, 1f, 0f);
            MatrixState2D.setProjectOrtho(-ratio, ratio, -1, 1, 1, 100);
            MatrixState2D.setLightLocation(0, 50, 0);

            if (currView == null) {
                LoadView lv = new LoadView(MySurfaceView.this);
                currView = lv;
                lv = null;
            }
        }
    }

}
